package com.huazhi.wzsnw.service.impl;

import com.huazhi.wzsnw.mapper.AlertMapper;
import com.huazhi.wzsnw.mapper.SmokeMapper;
import com.huazhi.wzsnw.model.Alert;
import com.huazhi.wzsnw.model.Smoke;
import com.huazhi.wzsnw.service.SmokeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

@Service
public class SmokeServiceImpl implements SmokeService
{

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private SmokeMapper smokeMapper;
    @Autowired
    private AlertMapper alertMapper;
    @Override
    public List<String> selectSmokeBySearch(String number, String state, String userTid,int degree,String switchTest,String companyTid,String type) {
        return smokeMapper.selectSmokeBySearch(number,state,userTid,degree,switchTest,companyTid,type);
    }

    @Override
    public List<Smoke> selectSmokeByTid(List<String> list, Integer pageIndex, Integer pageSize) {
        return smokeMapper.selectSmokeByTid(list,pageIndex,pageSize);
    }

    @Override
    public void updateByPrimaryKeySelective(Smoke smoke) {
        smokeMapper.updateByPrimaryKeySelective(smoke);
    }

    @Override
    public Smoke getSmokeByTid(String tid) {
        return smokeMapper.selectByPrimaryKey(tid);
    }

    public Integer smokeNumberByState(String tid,String state) {
        return smokeMapper.smokeNumberByState(tid,state);
    }

    @Override
    public Integer receiveAlert(String number, String state, String time) {
        Timestamp updateTime = null;
        if(time!=null){
            try{
                updateTime = new Timestamp(sdf.parse(time).getTime());
            }catch (Exception e){
                e.printStackTrace();
                updateTime = new Timestamp(System.currentTimeMillis());
            }
        }else{
            updateTime = new Timestamp(System.currentTimeMillis());
        }
        smokeMapper.updateStateByNumber(number,state,updateTime);

        Alert alert = new Alert();
        alert.setNumber(number);
        alert.setState(state);
        alert.setTid(UUID.randomUUID().toString());
        alert.setTime(updateTime);
        alert.setType("烟感报警");
        alertMapper.insertSelective(alert);

        return 1;
    }

    @Override
    public List<Smoke> getSomke(String tid) {
        return smokeMapper.getSomke(tid);
    }

    @Override
    public Smoke getSmokeByNumber(String number) {
        return smokeMapper.getSmokeByNumber(number);
    }

    @Override
    public List<Smoke> getSomkelAYindextopCard(String tid) {
        return smokeMapper.getSomkelAYindextopCard(tid);
    }

    @Override
    public Integer getDeviceCount(String tid, String type) {
        return smokeMapper.getDeviceCount(tid,type);
    }

    @Override
    public Integer getDeviceStateCount(String tid, String type, String state) {
        return smokeMapper.getDeviceStateCount(tid,type,state);
    }
}
